package com.neusoft.lohas.modules.ivm.entity.order;

import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.annotations.Where;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import com.neusoft.lohas.common.persistence.IdEntity;
import com.neusoft.lohas.modules.ivm.entity.business.ActivityProduct;
import com.neusoft.lohas.modules.ivm.entity.merchant.Merchant;
import com.neusoft.lohas.modules.ivm.entity.product.Product;
import com.neusoft.lohas.modules.sys.entity.User;

/**
 * 购物车Entity
 * 
 * @author Jackie.Liu
 * @version 2014-12-16
 */
@Entity
@Table(name = "ivm_shopping_cart")
@DynamicInsert
@DynamicUpdate
public class ShoppingCart extends IdEntity<ShoppingCart> {

	private static final long serialVersionUID = 1L;
	private Merchant merchant; // 所属商户
	private User user; // 所属用户
	private Product product; // 所属商品
	private int number;
	private double price;
	private double amount;
	private int type; //商品状态
	private List<ActivityProduct> actProductList = Lists.newArrayList(); //对应抢购商品信息集合
	private long countdownTime; //购物车有效时间（倒计时）
	
	public ShoppingCart() {
		super();
	}

	public ShoppingCart(String id) {
		this();
		this.id = id;
	}
	
	@ManyToOne
	@JoinColumn(name="merchant_id")
	@NotFound(action = NotFoundAction.IGNORE)
	@NotNull(message="归属商户不能为空")
	public Merchant getMerchant() {
		return merchant;
	}

	public void setMerchant(Merchant merchant) {
		this.merchant = merchant;
	}

	@ManyToOne
	@JoinColumn(name = "user_id")
	@NotFound(action = NotFoundAction.IGNORE)
	@NotNull(message="所属用户不能为空")
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@ManyToOne
	@JoinColumn(name = "product_id")
	@NotFound(action = NotFoundAction.IGNORE)
	@NotNull(message="所属商品不能为空")
	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public int getNumber() {
		return number;
	}

	public void setNumber(int number) {
		this.number = number;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public double getAmount() {
		return amount;
	}

	public void setAmount(double amount) {
		this.amount = amount;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	@ManyToMany(fetch = FetchType.LAZY, cascade = { CascadeType.ALL })
	@JoinTable(name = "ivm_business_panicbuy", joinColumns = { @JoinColumn(name = "cart_id") }, inverseJoinColumns = { @JoinColumn(name = "activity_product_id") })
	@Where(clause="del_flag='"+DEL_FLAG_NORMAL+"'")
	@OrderBy("id") @Fetch(FetchMode.SUBSELECT)
	@NotFound(action = NotFoundAction.IGNORE)
	@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	@JsonIgnore
	public List<ActivityProduct> getActProductList() {
		return actProductList;
	}

	public void setActProductList(List<ActivityProduct> actProductList) {
		this.actProductList = actProductList;
	}
	
	/**
	 * 获取商品失效时间
	 * =商品购物车有效时间-（系统当前时间-商品加入购物车时间）
	 */
	@Transient
	public long getCountdownTime(){
		countdownTime=0;//剩余时间
		long effectiveTime = Long.valueOf(this.product.getEffectiveTime())*3600*1000;//购物车有效时间
		long nowTime = new Date().getTime();//系统当前时间
		long updateTime = this.updateDate.getTime();//商品加入购物车时间
		countdownTime = effectiveTime-nowTime+updateTime;
		return countdownTime;
	}
	
	public void setCountdownTime(long countdownTime){
		this.countdownTime = countdownTime;
	}
	
}
